#! /bin/sh

set -e

HOME=`pwd`
BUILD_DIR="$HOME/build"
BIN_DIR="$HOME/bin"

runservice() {
    case $1 in
        server)
            ${BIN_DIR}/server/fileserver $2 $3;;
        client)
            ${BIN_DIR}/client/fileclient $2 $3;;
        *)
            echo "不合法的命令!"
            exit 0;;
    esac
}

if [ $# -gt 0 ];then
    # 带参数，启动服务
    if [ $# -ne 3 ]; then
        echo "usage: ./auto_build.sh [server/client] ip port"
    else
        runservice $1 $2 $3
    fi
else
    if [ -d $BUILD_DIR ]; then
        rm -rf $BUILD_DIR
    fi
    if [ -d $BIN_DIR ]; then
        rm -rf $BIN_DIR
    fi

    mkdir $BUILD_DIR
    mkdir -p ${BIN_DIR}/client
    mkdir -p ${BIN_DIR}/server

    cd $BUILD_DIR
    cmake ..
    make

    cd ${BIN_DIR}
    mv fileclient client
    mv fileserver server
fi
